import axios from 'axios';

//设置axios对象的默认值
axios.defaults.baseURL = 'http://localhost:8000/ylb';
axios.defaults.timeout = 50000

//封装get请求的函数
export function doGet(path, data) {
    return axios({
        url: path,
        method: 'GET',
        params: data
    })
}
//封装post请求
export function doPost(path,data){
    return axios({
        url:path,
        method:'POST',
        data:data
    })
}


export function upload(path,data){
    return axios({
        url:path,
        method:"POST",
        data:data,
        headers:{
            'Content-Type':'multipart/form-data'
        }
    })
}

//请求拦截器
axios.interceptors.request.use(config=>{
    let strToken=window.sessionStorage.getItem("ylb");
    if (strToken){
       //转成json对象
     let Tokenobj =  JSON.parse(strToken);
     let uid=Tokenobj.id;
     let token=Tokenobj.token;
     //修改config的error
        config.headers.Authorization='Bearer '+token;
        config.headers.uid=uid;
    }
    return config;
},error => {
    alert("请求拦截器错误:"+error)
})

//应答拦截器
axios.interceptors.response.use(resp => {
    //处理统一的应答结果
    if (resp && resp.data.code > 1000) {
        alert("提示:" + resp.data.msg);
    }
    return resp.data;

})